﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using YArchitech.Revit;
using YJKExcelFormTool.Utilities;

namespace YJKExcelFormTool.Kits
{
	public class FacePositionComparer
	{
		private static XYZ MinPoint(Face face, DrawLegendKit.ViewSectionType vsType)
		{
			List<XYZ> list = new List<XYZ>();
			foreach (object obj in face.EdgeLoops)
			{
				foreach (object obj2 in ((EdgeArray)obj))
				{
					Curve curve = ((Edge)obj2).AsCurve();
					list.Add(curve.GetEndPoint(0));
				}
			}
			list.Sort(delegate(XYZ pt1, XYZ pt2)
			{
				double num = 0.0;
				double value = 0.0;
				switch (vsType)
				{
				case DrawLegendKit.ViewSectionType.VST_Front:
					num = pt1.Y;
					value = pt2.Y;
					break;
				case DrawLegendKit.ViewSectionType.VST_Back:
					num = -pt1.Y;
					value = -pt2.Y;
					break;
				case DrawLegendKit.ViewSectionType.VST_Left:
					num = pt1.X;
					value = pt2.X;
					break;
				case DrawLegendKit.ViewSectionType.VST_Right:
					num = -pt1.X;
					value = -pt2.X;
					break;
				case DrawLegendKit.ViewSectionType.VST_Top:
					num = -pt1.Z;
					value = -pt2.Z;
					break;
				case DrawLegendKit.ViewSectionType.VST_Bottom:
					num = pt1.Z;
					value = pt2.Z;
					break;
				}
				return num.CompareTo(value);
			});
			return list[0];
		}

		public static int Compare(Face lhv, Face rhv, DrawLegendKit.ViewSectionType vsType)
		{
			XYZ xyz = FacePositionComparer.MinPoint(lhv, vsType);
			XYZ xyz2 = FacePositionComparer.MinPoint(rhv, vsType);
			double val = 0.0;
			double val2 = 0.0;
			switch (vsType)
			{
			case DrawLegendKit.ViewSectionType.VST_Front:
				val = xyz.Y;
				val2 = xyz2.Y;
				break;
			case DrawLegendKit.ViewSectionType.VST_Back:
				val = -xyz.Y;
				val2 = -xyz2.Y;
				break;
			case DrawLegendKit.ViewSectionType.VST_Left:
				val = xyz.X;
				val2 = xyz2.X;
				break;
			case DrawLegendKit.ViewSectionType.VST_Right:
				val = -xyz.X;
				val2 = -xyz2.X;
				break;
			case DrawLegendKit.ViewSectionType.VST_Top:
				val = -xyz.Z;
				val2 = -xyz2.Z;
				break;
			case DrawLegendKit.ViewSectionType.VST_Bottom:
				val = xyz.Z;
				val2 = xyz2.Z;
				break;
			}
			if (Geometry.IsEqual(val, val2))
			{
				double area = lhv.Area;
				double area2 = rhv.Area;
				if (Geometry.IsEqual(area, area2))
				{
					return 0;
				}
				if (Geometry.LessThan(area, area2))
				{
					return -1;
				}
			}
			else if (Geometry.LessThan(val, val2))
			{
				return -1;
			}
			return 1;
		}
	}
}
